﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using BAL.EstRec;
using BAL.EstRec.HQConsolidacion;
using BAL.EstRec.Common;
using System.Net;
using Microsoft.Win32;

namespace Nav3
{
    /// <summary>
    /// Lógica de interacción para ConsolidacionHQ.xaml
    /// </summary>
    public partial class ConsolidacionHQ : UserControl, InterfazControl,IDlgLoader,IConsolidacionHQUIRefresh,IDlgConsolidateConfirmation,IFileChooser
    {
        public bool IsModified { get; set; }
        private VMHqCon vmodel;
        

        public ConsolidacionHQ()
        {
            InitializeComponent();
            try
            {
                this.vmodel = new VMHqCon(0, this);
                this.DataContext = this.vmodel;
                this.dataGridConsolidacion.ItemsSource = this.vmodel.entries;
            }
            catch(Exception ex)
            {
                string msg = "No se pudo cargar la información de la base de datos. Valide que cuenta con los permisos necesarios.\nDetalles : "+ex.Message;
                MessageBox.Show("", "Consolidación", MessageBoxButton.OK, MessageBoxImage.Error);
                this.btnConsolidar.IsEnabled = false;
                this.btnExportarXls.IsEnabled = false;
            }
            
        }

        public void loadData(long unidVersion)
        {
            this.vmodel.loadData(unidVersion);
            this.dataGridConsolidacion.ItemsSource = this.vmodel.entries;
        }

        private void btnConsolidar_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.vmodel.consolidate(this,this);       
            }
            catch (WebException)
            {
                MessageBox.Show("No se puede realizar la conexión a WorkPoint.\r Verifique su conexión a la intranet de PMI y vuelva intentarlo más tarde.", "Aviso", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void btnExportarXls_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.vmodel.exportToExcel(this, this);
            }
            catch (Exception ex)
            {
                MessageBox.Show("No se pudo crear el archivo.\nDetalles : "+ex.Message, "Aviso", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        public void show(BAL.EstRec.Common.IDlgLoaderDataContext dataContext)
        {
            DlgSave ds = new DlgSave();
            ds.DataContext = dataContext;
            ds.Owner = Window.GetWindow(this);
            ds.ShowDialog();
        }

        public void loadDataFromUIThread()
        {
            this.Dispatcher.BeginInvoke(new Action(() =>
            {
                this.vmodel.loadData();
            }), System.Windows.Threading.DispatcherPriority.Normal, null);
        }

        public bool getUserConfirmation(string message)
        {
            bool answer=false;
            var res=MessageBox.Show(message, "Confirmación", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
            if (res == MessageBoxResult.Yes)
            {
                answer=true;
            }

            return answer;
        }

        public void showConsolidateRefuse(string message)
        {
            MessageBox.Show(message, "Consolidación", MessageBoxButton.OK, MessageBoxImage.Stop);
        }

        public string getPath(string filter)
        {
            string fullFilePath="";
            try
            {
                SaveFileDialog saveDialog = new SaveFileDialog();
                saveDialog.Filter=filter;
                if(saveDialog.ShowDialog().Value)
                {
                    fullFilePath=saveDialog.FileName;
                }
                this.disableMouseUI();
            }
            catch(Exception ex)
            {
                fullFilePath="";
            }
            return fullFilePath;
        }

        public void endProcess()
        {
            this.enableMouseUI();
        }

        private void disableMouseUI()
        {
            this.IsHitTestVisible = false;
            Mouse.OverrideCursor = Cursors.Wait;
        }

        public void enableMouseUI()
        {
            this.IsHitTestVisible = true;
            Mouse.OverrideCursor = Cursors.Arrow;
        }



    }
    public class CapConHQ
    {
        public string Column1 { set; get; }
        public string Column2 { set; get; }
        public string Column3 { set; get; }
       
    }
}
